package Test.tools;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

public class Condatabase {

	public static ArrayList<Object> getTableNameByCon(Connection con) {

		ArrayList<Object>  aray=new ArrayList<Object>();
		try {
			DatabaseMetaData meta = con.getMetaData();
			ResultSet rs = meta.getTables(null, null, null, new String[] { "TABLE" });
			PreparedStatement stmt;
			while (rs.next()) {
				String tableName=rs.getString(3);
				System.out.println("已处理的表：" + rs.getString(3));
				System.out.println("------------------------------");
				String sql="select * from "+rs.getString(3);
				stmt = con.prepareStatement(sql);
				ResultSet rss = stmt.executeQuery(sql);
				ResultSetMetaData data = rss.getMetaData();
				String columArray="";
				for (int i = 1; i <= data.getColumnCount(); i++) {
					String columnName = data.getColumnName(i);
					columArray+="\""+columnName+"\",";
				}
				String ervry="{\""+tableName+"\","+columArray;
				ervry=ervry.substring(0, ervry.length()-1)+"}";
				aray.add(ervry);
			}
			con.close();
		} catch (Exception e) {
			try {
				con.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return aray;
	}


	public static void main(String[] args) throws SQLException {
		
		// sysoutStrTablePdmCloumns("CT_INFO_CONTRACT_BORROW","DEV");
		Connection coon = Condatabase.getConnection();
		ArrayList<Object> er=Condatabase.getTableNameByCon(coon);
		System.out.println("==="+er);
	}
	
	
	public static Connection getConnection() {
		
		final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
		final String DATABASE_URL = "jdbc:mysql://localhost:3306/iaims?useUnicode=true";
		final String DATABASE_USER = "root	";
		final String DATABASE_PASSWORD = "1234";
		Connection con = null;
		try {
			Class.forName(DRIVER_CLASS);
			con = DriverManager.getConnection(DATABASE_URL, DATABASE_USER,DATABASE_PASSWORD);
			return con;
		} catch (Exception ex) {
			System.out.println("2:" + ex.getMessage());
		}
		return con;
	}

}